<?php

class Db_Tables2_DccUsersCallStatus
{
    function __construct() {
		$this->maozhua_wb = Zend_Registry::get("maozhua_wb");
	}
	
	function InitCompanyUsers($company_id)
	{
		$select = $this->maozhua_wb->select();
		$select->from("dcc_users", array("user_id"));
		$select->where("company_id = ?", $company_id);
		$select->where("user_type = ?", 2);

		$rows = $this->maozhua_wb->fetchAll($select);
		
		$user_ids = array();
		
		if(!empty($rows))
		{
			foreach($rows as $row)
			{
				$user_ids[$row['user_id']] = $row['user_id'];
			}
		}
		
		if(!empty($user_ids))
		{
			$select = $this->maozhua_wb->select();
			$select->from("dcc_users_call_status", array("user_id"));
			$select->where("user_id IN (?)", $user_ids);

			$rows = $this->maozhua_wb->fetchAll($select);
			
			if(!empty($rows))
			{
				foreach($rows as $row)
				{
					unset($user_ids[$row['user_id']]);
				}
			}
		}
		
		if(!empty($user_ids))
		{
			$data = array(
				"is_online" => 0,
				"is_busy" => 0
			);
			
			foreach($user_ids as $user_id)
			{
				$data['user_id'] = $user_id;
				
				$this->maozhua_wb->insert("dcc_users_call_status", $data);
			}
		}
		
		return true;
	}
}